﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for NhanVienDAL
/// </summary>
public class NhanVienDAL
{
    public static object IsNull(object checkValue, object replaceValue)
    {
        if (checkValue.Equals(System.DBNull.Value) || checkValue == null)
            return replaceValue;
        return checkValue;
    }

    public static List<NhanVienINFO> listAll()
    {
        string sql = "select * from NhanVien";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetListObjects<NhanVienINFO>(dr);
    }

    public static NhanVienINFO GetInfo(int MaNV)
    {
        string sql = "select * from NhanVien where MaNV=" + MaNV ;
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetObject<NhanVienINFO>(dr);
    }

    public static List<int> NV_CN(int MaNV)
    {
        string sql = "select * from CN_NV where MaNV=" + MaNV ;
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        List<int> list = new List<int>();
        while (dr.Read())
        {
            int Ma = Convert.ToInt32(IsNull(dr["MaCN"], 0));
           list.Add(Ma);
        }
        dr.Close();
        return list;
    }

    //public static int Insert(NhanVienINFO obj)
    //{
    //    obj.MaNV = GetMa();
    //    obj.MaNV = obj.MaNV + 1;
    //    string sql = "INSERT INTO HOCVIEN (MaHV,TenHV,GioiTinh,NgaySinh,DiaChi,SDT,Email) VALUES (" + obj.MaHV + ",N'" + obj.TenHV + "'," + obj.GioiTinh + " ,'" + obj.NgaySinh + "',N'" + obj.DiaChi + "','" + obj.SDT + "','" + obj.Email + "')";
    //    return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    //}

    //public static int Update(NhanVienINFO obj)
    //{
    //    string sql = "Update HOCVIEN set TenHV=N'" + obj.TenHV + "', GioiTinh=" + obj.GioiTinh + ", NgaySinh='" + obj.NgaySinh + "',DiaChi=N'" + obj.DiaChi + "',SDT='" + obj.SDT + "',Email='" + obj.Email + "' Where MaHV=" + obj.MaHV + "";
    //    return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    //}

    //public static int Delete(int mahocvien)
    //{
    //    string sql = "Delete from HOCVIEN where MaHV =" + mahocvien + "";
    //    return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    //}

    //public static int GetMa()
    //{
    //    string sql = "SELECT ISNULL(MAX(MaHV),0) FROM HOCVIEN";
    //    SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
    //    int ma = 0;
    //    if (dr.Read())
    //    {
    //        ma = int.Parse(dr[0].ToString());
    //    }
    //    dr.Close();
    //    return ma;
    //}

    public static NhanVienINFO DangNhap(string email)
    {
        string sql = "select * from HOCVIEN where email='" + email + "'";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetObject<NhanVienINFO>(dr);
    }
}